package com.woniuxy.main.configuration;

import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.woniuxy.main.realm.UserRealm;
import com.woniuxy.main.service.UserService;

@Configuration  //将当前类指定为配置类 等价于shiro的xml
public class ShiroConfiguration {
	//配置realm
	@Bean
	public UserRealm getReam(){
		UserRealm realm=new UserRealm();
		System.out.println("创建realm:"+realm);
		return  realm;
		
	}
	//安全管理器
	@Bean
	public DefaultWebSecurityManager getManager(UserRealm realm,DefaultWebSessionManager session){
		System.out.println("manager："+realm);
		DefaultWebSecurityManager manager =new DefaultWebSecurityManager();
		manager.setRealm(realm);
		manager.setSessionManager(session);
		return manager;
				
	}
	
	//过滤器
	@Bean
	public ShiroFilterFactoryBean getFactory(DefaultWebSecurityManager manager){
		System.out.println("创建过滤器"+manager);
		ShiroFilterFactoryBean bean =new ShiroFilterFactoryBean();
		bean.setSecurityManager(manager);
		//指定登录页面的url：shiro判断当前用户没有登录就自动跳转到登录页面
		bean.setLoginUrl("/index.html");
		//当前用户没有权限操作时跳转的页面
		bean.setUnauthorizedUrl("/index.html");
		//过滤器链
		Map<String, String> map=new LinkedHashMap<>();//需要保证有序
		map.put("/index.html", "anon");
		map.put("/html/register.html", "anon");
		map.put("/user/login", "anon");
		map.put("/user/sendMail", "anon");
		map.put("/user/mail", "anon");
		map.put("/user/creatYZM", "anon");
		map.put("/user/confirmYZM", "anon");
		map.put("/user/register", "anon");
		map.put("/user/loginByAccount", "anon");
		map.put("/PictureCode/loginCode", "anon");
		map.put("/user/loginByPhone", "anon");
		map.put("/user/loginByMail", "anon");
		map.put("/user/judgeCode", "anon");
		map.put("/user/changePwdByPhone", "anon");
		map.put("/user/changePwdByMail", "anon");
		map.put("/user/findUserByUid", "anon");
		map.put("/image/**", "anon");
		map.put("/images/**", "anon");
		map.put("/css/**", "anon");
		map.put("/js/**", "anon");
		map.put("/assets/**", "anon");
		map.put("/bootstrap-4.0.0/**", "anon");
		map.put("/bootstrap-4.1.0/**", "anon");
		map.put("/icon/**", "anon");
		map.put("/jsp/**", "anon");
		map.put("/layui/**", "anon");
		map.put("/webfonts/**", "anon");
		map.put("/logout", "logout");
		map.put("/**", "authc");
		bean.setFilterChainDefinitionMap(map);
		return bean;
	}
	//session管理器
	@Bean
	public DefaultWebSessionManager getSession(){
		System.out.println("创建session");
		DefaultWebSessionManager session =new DefaultWebSessionManager();
		
		return session;
	}
	//注销监听
	
}
